class Solution {
public:
    vector<int> postorderTraversal(TreeNode *root) {
        vector<int> res;
        if (root == nullptr) {
            return res;
        }

        stack<TreeNode *> stk;
        TreeNode *prev = nullptr;
        while (root != nullptr || !stk.empty()) {
            while (root != nullptr) {
                stk.emplace(root);
                root = root->left;
            }
            root = stk.top();
            stk.pop();
			if(root->right==nullptr || root->right==prev)
            {
				res.push_back(root->val);
				prev = root;
				root = nullptr;
            }
            else
            {
                stk.push(root);
                root = root->right;
            }
        }
        return res;
    }
};